﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Middle_1405_LongestDiverseString
{
    class MainPorject
    {
        static void Main(string[] args)
        {
            Console.WriteLine(LongestDiverseString1(1, 1, 7));
            Console.ReadKey();
        }

        public static string AddStrings(string num1, string num2)
        {
            BigInteger num11 = BigInteger.Parse(num1);
            BigInteger num22 = BigInteger.Parse(num2);
            return (num11 + num22).ToString();
        }

        public static string LongestDiverseString(int a, int b, int c)
        {
            StringBuilder res = new StringBuilder();
            Pair[] arr = { new Pair(a, 'a'), new Pair(b, 'b'), new Pair(c, 'c') };

            while (true)
            {
                Array.Sort(arr, (p1, p2) => p2.Freq - p1.Freq);
                bool hasNext = false;
                foreach (Pair pair in arr)
                {
                    if (pair.Freq <= 0)
                    {
                        break;
                    }
                    int m = res.Length;
                    if (m >= 2 && res[m - 2] == pair.Ch && res[m - 1] == pair.Ch)
                    {
                        continue;
                    }
                    hasNext = true;
                    res.Append(pair.Ch);
                    pair.Freq--;
                    break;
                }
                if (!hasNext)
                {
                    break;
                }
            }

            return res.ToString();
        }

        class Pair
        {
            public int Freq { get; set; }
            public char Ch { get; set; }

            public Pair(int Freq, char Ch)
            {
                this.Freq = Freq;
                this.Ch = Ch;
            }
        }



        public static string LongestDiverseString1(int a, int b, int c)
        {
            StringBuilder str = new StringBuilder();
            Pair[] pairs = { new Pair(a, 'a'), new Pair(b, 'b'), new Pair(c, 'c'), };

            while (true)
            {
                Array.Sort(pairs, (p1, p2) => p2.Freq - p1.Freq);
                bool isNext = false;
                foreach (var pair in pairs)
                {
                    if (pair.Freq<=0)
                    {
                        break;
                    }
                    int m = str.Length;
                    if (m>2)
                    {
                        if (pair.Ch == str[m - 1] && str[m - 2]==pair.Ch)
                        {
                            continue;
                        }
                    }
                    isNext = true;
                    str.Append(pair.Ch);
                    pair.Freq--;
                    break;
                }
                if (!isNext)
                {
                    break;
                }
            }
            return str.ToString();
        }

        private static bool IsComfirm(string str)
        {
            if (str.Length <= 0) return true;
            int index = 0;
            char curChar = str[0];
            foreach (var item in str)
            {
                if (item == curChar)
                {
                    index++;
                    if (index >= 3)
                    {
                        return false;
                    }
                }
                else
                {
                    curChar = item;
                    index = 1;
                }
            }
            return true;
        }

        private static Queue<string> GetMaxQue(Queue<string> aQue, Queue<string> bQue, Queue<string> cQue, string str = null)
        {
            if (/*(aQue.Count > 0 && aQue.Peek() != str) && */aQue.Count >= bQue.Count && aQue.Count >= cQue.Count) return aQue;
            if (/*(bQue.Count > 0 && bQue.Peek() != str) && */bQue.Count >= aQue.Count && bQue.Count >= cQue.Count) return bQue;
            if (/*(cQue.Count > 0 && cQue.Peek() != str) && */cQue.Count >= bQue.Count && cQue.Count >= aQue.Count) return cQue;
            return null;
        }
        private static Queue<string> GetMiddleQue(Queue<string> aQue, Queue<string> bQue, Queue<string> cQue)
        {
            if ((cQue.Count >= bQue.Count && cQue.Count <= aQue.Count) || (cQue.Count <= bQue.Count && cQue.Count >= aQue.Count)) return cQue;
            if ((bQue.Count >= aQue.Count && bQue.Count <= cQue.Count) || (bQue.Count <= aQue.Count && bQue.Count >= cQue.Count)) return bQue;
            if ((aQue.Count >= bQue.Count && aQue.Count <= cQue.Count) || (aQue.Count <= bQue.Count && aQue.Count >= cQue.Count)) return aQue;
            return null;
        }
        private static Queue<string> GetMinQue(Queue<string> aQue, Queue<string> bQue, Queue<string> cQue)
        {
            if (aQue.Count <= bQue.Count && aQue.Count <= cQue.Count) return aQue;
            if (bQue.Count <= aQue.Count && bQue.Count <= cQue.Count) return bQue;
            if (cQue.Count <= bQue.Count && cQue.Count <= aQue.Count) return cQue;
            return null;
        }
    }
}
